package com.tech.connect.zhaorencai.zhuanwaikuai;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.ksy.common.utils.BaseAdapter;
import com.ksy.common.utils.BaseHolder;
import com.ksy.common.view.HLineDivider;
import com.ksy.common.view.ProxyLayout;
import com.ksy.common.view.PullToRefreshLayout;
import com.tech.connect.R;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.ZhuanWaiKuaiHttp;
import com.tech.connect.fragment.BaseFragment;
import com.tech.connect.model.ItemRenMai;
import com.tech.connect.model.PagerModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ZiJinTuoGuanFragment extends BaseFragment {

    private RecyclerView recycler;
    private BaseAdapter<String, BaseHolder> adapter;
    private ProxyLayout<RecyclerView> proxyLayout;

    @Override
    public View onCreateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_zhuanwaikuai_zijintuoguan, null);
        return view;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        loadData();
    }

    private void initView(View view) {
        recycler = view.findViewById(R.id.recycler);
        proxyLayout = new ProxyLayout<>(activity, recycler);
        proxyLayout.setCanRefresh(true);
        proxyLayout.setDragListener(new PullToRefreshLayout.DragListener() {
            @Override
            public void drag2Upper(PullToRefreshLayout pullToRefreshLayout) {
                ++page;
                loadData();
            }

            @Override
            public void drag2Downer(PullToRefreshLayout pullToRefreshLayout) {
                page = 1;
                loadData();
            }
        });
        recycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
        recycler.addItemDecoration(new HLineDivider(10f));

    }

    private void initAdapter() {
        List<String> allList = new ArrayList<>();
        if (adapter == null) {
            adapter = new BaseAdapter<String, BaseHolder>(R.layout.item_zhuanwaikuai_zijintuoguan, allList, 6) {
                @Override
                protected void convert(BaseHolder helper, int position) {
                    TextView tvCompany = helper.getView(R.id.tvCompany);
                    tvCompany.setText("海通证券大厦三十五楼");
                    RecyclerView itemRecycler = helper.getView(R.id.itemRecycler);
                    RecyclerView.LayoutManager layoutManager = itemRecycler.getLayoutManager();
                    if (layoutManager == null) {
                        itemRecycler.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false) {
                            @Override
                            public boolean canScrollVertically() {
                                return false;
                            }
                        });
                        itemRecycler.addItemDecoration(new HLineDivider());
                    }
                    RecyclerView.Adapter adapter = itemRecycler.getAdapter();
                    BaseAdapter<String, BaseHolder> baseAdapter;
                    List<String> items = new ArrayList<>();
                    if (adapter == null || !(adapter instanceof BaseAdapter)) {
                        baseAdapter = new BaseAdapter<String, BaseHolder>(R.layout.item_layout_zijintuoguan, items, 2) {
                            @Override
                            protected void convert(BaseHolder helper, int position) {
                                TextView tvDate = helper.getView(R.id.tvDate);
                                tvDate.setText(position + "期");
                                TextView tvMoney = helper.getView(R.id.tvMoney);
                                tvMoney.setText("6000");
                            }
                        };
                    } else {
                        baseAdapter = (BaseAdapter) adapter;
                    }
                    itemRecycler.setAdapter(baseAdapter);
                    baseAdapter.notifyDataListChanged(items);
                }
            };
            recycler.setAdapter(adapter);
        }
        adapter.notifyDataSetChanged();
    }


    private int page = 1;
    private int pageSize = 15;
    private void loadData() {
        Map<String, Object> map = ZhuanWaiKuaiHttp.getMap();
//        map.put("areaCode", 1);
//        map.put("cityCode", 1);
//        map.put("provinceCode", 1);
//        map.put("industryCategoryId", 1);
//        map.put("maxDate", 0);
//        map.put("minDate", 0);
        map.put("pageNumber", page);
        map.put("pageSize", pageSize);
        ZhuanWaiKuaiHttp.zijintuoguan(map, new BaseEntityOb<PagerModel<ItemRenMai>>() {
            @Override
            public void onDataDeal(boolean isSuccess, PagerModel<ItemRenMai> data, String msg) {
//                List<ItemQuznZi> list = null;
//                if (isSuccess && data != null) {
//                    list = data.content;
//                }
//                if (page == 1) {
//                    allList.clear();
//                }
//                //initAdapter(list);
//                if (list != null && list.size() >= pageSize) {
//                    proxyLayout.setCanLoadMore(true);
//                } else {
//                    proxyLayout.setCanLoadMore(false);
//                }
//                if (allList.isEmpty()) {
//                    proxyLayout.showEmptyView();
//                } else {
//                    proxyLayout.showContentView();
//                }
//                proxyLayout.dragFinish();
            }
        });
    }
}